<?php

namespace App\Repositories\Goods;

use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Repositories\Goods\GoodsBookRepository;
use App\Models\Goods\GoodsBook;
use App\Validators\Goods\GoodsBookValidator;

/**
 * Class GoodsBookRepositoryEloquent.
 *
 * @package namespace App\Repositories\Goods;
 */
class GoodsBookRepositoryEloquent extends BaseRepository implements GoodsBookRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return GoodsBook::class;
    }

    

    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

    /**
     * 判断缺货是否已登记
     * @author Jerry
     * @param int $goodsId
     * @param int $userId
     * @return mixed
     */
    public function findGoodsBookCountByGoodsId(int $goodsId, int $userId)
    {
        $where = [
            'user_id'   =>  $userId,
            'is_send_m' =>  0,
            'goods_id'  =>  $goodsId,
            [
                'booking_time',
                '>',
                strtotime('-3 days')
            ],
        ];

        $expiresAt = Carbon::now()->addDay(1);

        return Cache::remember("goods:GoodsBook{$goodsId}UserId{$userId}", $expiresAt, function () use ($where) {
            return $this->findWhere($where)->count();
        });
    }
    
}
